<?php
/**
 * Created by JetBrains PhpStorm.
 * User: onwer
 * Date: 11-8-31
 * Time: 下午9:11
 * To change this template use File | Settings | File Templates.
 */
 
class PHPFetion {
    protected $cookie;
    public $isLogin;
    public function __construct(){
        $this->isLogin=false;
        $this->cookie = dirname(__FILE__).'/cookie.txt';
    }
    public function login($phone,$pass){
        $post=array(
                            'm' => $phone,
                            'pass' => $pass,
                            'loginstatus' => 1
                        );
        $url='http://f.10086.cn/im/login/inputpasssubmit1.action';
        $cookie=$this->cookie;
        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie); // 注意这里！保存Cookie
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));
        $result = curl_exec($curl);
        curl_close($curl);
        $error=array('手机号不是中国移动手机号,请输入中国移动手机号登录WAP飞信!',
                        '对不起,密码输入错误,请重新输入!',
                        '您正在登录中国移动WAP飞信,请稍候…');
        if($this->check($result,$error)==2){
            $this->isLogin=true;
        }
    }
    public function sendtoSelf($msg){
        $suss=array('短信发送成功!');
        $post = array('msg' => $msg);
        $url='http://f.10086.cn/im/user/sendMsgToMyselfs.action';
        if($this->isLogin){
            $r=$this->postSend($url,$post,$this->cookie);
            if($this->check($r,$suss)===0){
                return true;
            }
        }

        return false;
    }
    protected function check($r,$m){
        for($i=0;$i<count($m);$i++){
            if(strpos($r,$m[$i])!==false){
                return $i;
            }
        }
    }
    protected function postSend($url,$post,$cookie){
        $curl = curl_init($url);
            curl_setopt($curl, CURLOPT_HEADER, 0);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie);    // 注意这里！
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));
            $result = curl_exec($curl);
            curl_close($curl);
        return $result;
    }
    protected function getSend($url,$cookie){
        $curl = curl_init($url);
            curl_setopt($curl, CURLOPT_HEADER, 0);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie);    // 注意这里！
            $result = curl_exec($curl);
            curl_close($curl);
        return $result;
    }
    protected function getUserId($pnum){
        $url='http://f.10086.cn/im/index/searchOtherInfoList.action';
        $post = array('searchText' => $pnum);
        if($this->isLogin)
            $r=$this->postSend($url,$post,$this->cookie);

        preg_match('/toinputMsg\.action\?touserid=(\d+)/si', $r, $matches);
        $userid = isset($matches[1]) ? $matches[1] : '';
        return $userid;
    }
    public function sendtoFriend($pnum,$msg,$uid=false){
        $userid=$uid!==false?$uid:$this->getUserId($pnum);
        $url='http://f.10086.cn/im/chat/sendMsg.action?touserid='.$userid;

        $post = array( 'msg' => $msg);
        $suss=array('发送消息成功!');
        if($this->isLogin){
            $r=$this->postSend($url,$post,$this->cookie);
            if($this->check($r,$suss)===0){
                return true;
            }
        }

        return false;
    }
    public function loginOut(){
        $url='http://f.10086.cn/im/index/logoutsubmit.action';
        $r=$this->getSend($url,$this->cookie);
        $suss=array('您已经成功退出WAP飞信,欢迎您再次使用WAP飞信产品.');
        if($this->check($r,$suss)===0){
            return true;
        }
        return false;
    }
    public function getFriendList(){
        $url='http://f.10086.cn/im/index/index.action?type=all&page=';
        $s='<ul id="frendList">';
        $r='';
        if($this->isLogin&&!isset($_SESSION['flist'])){
            for($p=1;$p<20;$p++){
                $rt=$this->getSend($url.$p,$this->cookie);
                $r.=$rt;
                if(strpos($rt,'下一页')===false){
                    break;
                }
            }



            preg_match_all('/<a.*?touserid\=(\d+)&amp;type.*?>(.*?)<\/a>/',$r,$m);
            for($i=0;$i<count($m[1]);$i++){
                $s.=sprintf('<li><a href="?uid=%s">%s</a></li>',$m[1][$i],$m[2][$i]);
            }
            $s.='</ul>';
            $_SESSION['flist']=$s;
            return $s;
        }else if($this->isLogin){
            return $_SESSION['flist'];
        }
    }

}
